package com.zhijian.medical.service.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.zhijian.medical.dao.local.MeetingDataMapper;
import com.zhijian.medical.entity.po.MeetingData;
import com.zhijian.medical.entity.vo.request.MeetingDataSaveOrUpdateVo;
import com.zhijian.medical.entity.vo.request.MeetingDataSearchVo;
import com.zhijian.medical.entity.vo.response.MeetingDataVo;
import com.zhijian.medical.service.MeetingDataService;
import com.zhijian.medical.util.Page;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Objects;

/**
 * 会议表格数据业务处理层
 *
 * @author auto
 * @version: 1.0
 * @date 2024-01-10 16:58:39
 */
@Slf4j
@Service
public class MeetingDataServiceImpl implements MeetingDataService {

    @Autowired
    private MeetingDataMapper mapper;

    @Override
    public MeetingData selectById(Long id) {
        return mapper.selectByPrimaryKey(id);
    }

    @Override
    public MeetingData selectByMeetingId(Long id) {
        return mapper.selectByMeetingId(id);
    }

    @Override
    public MeetingDataVo selectVoById(Long id) {
        return mapper.selectVoById(id);
    }

    @Override
    public void deleteById(Long id) {
        mapper.deleteByPrimaryKey(id);
    }

    @Override
    public void deleteByIdList(List<Long> idList) {
        if (CollectionUtil.isEmpty(idList)) {
            return;
        }
        mapper.deleteByIdList(idList);
    }

    @Override
    public Long insert(MeetingData meetingData) {
        Date now = new Date();
        meetingData.setCreateTime(now);
        meetingData.setUpdateTime(now);
        return mapper.insert(meetingData) > 0 ? meetingData.getId() : 0L;
    }

    @Override
    public boolean update(MeetingData meetingData) {
        Date now = new Date();
        meetingData.setUpdateTime(now);
        return mapper.updateByPrimaryKey(meetingData) != 0;
    }

    @Override
    public boolean updateByPrimaryKeySelective(MeetingData record) {
        Date now = new Date();
        record.setUpdateTime(now);
        return mapper.updateByPrimaryKeySelective(record) != 0;
    }

    @Override
    public boolean save(MeetingDataSaveOrUpdateVo saveOrUpdateVo) {
        MeetingData meetingData = new MeetingData();
        BeanUtils.copyProperties(saveOrUpdateVo, meetingData);
        return this.insert(meetingData) != 0;
    }

    @Override
    public boolean saveOrUpdate(MeetingDataSaveOrUpdateVo meetingDataSaveOrUpdateVo) {
        MeetingData meetingData = this.selectByMeetingId(meetingDataSaveOrUpdateVo.getMeetingId());
        if (Objects.isNull(meetingData)) {
            //保存
            return this.save(meetingDataSaveOrUpdateVo);
        } else {
            //修改
            return this.modify(meetingDataSaveOrUpdateVo);
        }
    }

    @Override
    public boolean modify(MeetingDataSaveOrUpdateVo saveOrUpdateVo) {
        MeetingData old = this.selectByMeetingId(saveOrUpdateVo.getMeetingId());
        old.setDate(saveOrUpdateVo.getDate());
        old.setTime(saveOrUpdateVo.getTime());
        old.setAddress(saveOrUpdateVo.getAddress());
        old.setTeacher(saveOrUpdateVo.getTeacher());
        old.setTitle(saveOrUpdateVo.getTitle());
        old.setGpsYjsSign(saveOrUpdateVo.getGpsYjsSign());
        old.setDoctorSign(saveOrUpdateVo.getDoctorSign());
        old.setJxsSign(saveOrUpdateVo.getJxsSign());
        old.setContent(saveOrUpdateVo.getContent());
        old.setDpPoint(saveOrUpdateVo.getDpPoint());
        old.setJxsSign(saveOrUpdateVo.getJxsSign());
        old.setTeacherSign(saveOrUpdateVo.getTeacherSign());
        old.setJlrSign(saveOrUpdateVo.getJlrSign());
        return this.update(old);
    }

    @Override
    public Integer count(MeetingDataSearchVo meetingDataSearchVo) {
        return mapper.count(meetingDataSearchVo);
    }

    @Override
    public List<MeetingDataVo> selectList(MeetingDataSearchVo meetingDataSearchVo) {
        return mapper.selectList(meetingDataSearchVo);
    }

    @Override
    public Page<MeetingDataVo> selectPage(MeetingDataSearchVo meetingDataSearchVo) {
        //先直接查总数
        int count = count(meetingDataSearchVo);
        //分页
        Page<MeetingDataVo> page = new Page<>(true, count, meetingDataSearchVo.getPageNum(), meetingDataSearchVo.getPageSize());
        meetingDataSearchVo.setStartIndex(page.getStartIndex());
        page.setPage(true);
        page.setTotalCount(count);
        List<MeetingDataVo> responseVos = this.selectList(meetingDataSearchVo);
        page.setData(responseVos);
        return page;
    }

    @Override
    public boolean insertBatch(List<MeetingData> entities) {
        return CollectionUtil.isNotEmpty(entities) && mapper.insertBatch(entities) > 0;
    }

    @Override
    public void deleteByCondition(MeetingDataSearchVo meetingDataSearchVo) {
        //慎重用此方法！！
        mapper.deleteByCondition(meetingDataSearchVo);
    }

    @Override
    public MeetingDataVo selectVoByMeetingId(Long meetingId) {
        return mapper.selectVoByMeetingId(meetingId);
    }

}